package com.yilin.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.platform.base_core.commons.exception.BusinessException;
import com.yilin.platform.entity.user.PlatConfigure;
import com.yilin.platform.entity.user.deserialize.Account;
import com.yilin.platform.entity.wlhy.TmsRootData;
import com.yilin.platform.mapper.user.PlatConfigureMapper;
import com.yilin.platform.mapper.wlhy.TmsRootDataMapper;
import com.yilin.platform.service.IPlatConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional
public class PlatConfigService extends ServiceImpl<PlatConfigureMapper, PlatConfigure> implements IPlatConfigService {

    @Resource
    PlatConfigureMapper tmsConfigureMapper;
    @Resource
    TmsRootDataMapper tmsRootDataMapper;

    public PlatConfigure getTmsConfigure() {
        return tmsConfigureMapper.selectOne(new QueryWrapper<>());
    }

    @Override
    public PlatConfigure initTmsConfigure(Account loginUser) {
        PlatConfigure tmsConfigure = getTmsConfigure();
        if (tmsConfigure != null) return tmsConfigure;
        if (loginUser==null) throw new BusinessException("未初始化登录");
        tmsConfigure = new PlatConfigure();
        tmsConfigure.setPlatId(loginUser.getOrganizeId());
        tmsConfigure.setPlatName(loginUser.getOrganizeName());
        tmsConfigure.setRemindPhone(loginUser.getPhone());
        tmsConfigureMapper.insert(tmsConfigure);
        return tmsConfigure;
    }

    @Override
    public PlatConfigure updateTmsConfigure(PlatConfigure query) {
        PlatConfigure tmsConfigure = getTmsConfigure();
        if (tmsConfigure == null) throw new BusinessException("请先初始化配置");
        tmsConfigure.setSendInterval(query.getSendInterval());
        tmsConfigure.setRemindInterval(query.getRemindInterval());
        tmsConfigure.setRemindPhone(query.getRemindPhone());
        tmsConfigure.setLastSentTime(query.getLastSentTime());
        tmsConfigure.setLastSentCount(query.getLastSentCount());
        tmsConfigure.setWaitSentCount(query.getWaitSentCount());
        tmsConfigure.setSentFailCount(query.getSentFailCount());
        tmsConfigure.setSentSuccessCount(query.getSentSuccessCount());
        tmsConfigureMapper.updateById(tmsConfigure);
        return tmsConfigure;
    }

    @Override
    public void updateLastCount(int count) {
        PlatConfigure tmsConfigure = getTmsConfigure();
        tmsConfigure.setLastSentCount(tmsConfigure.getSentFailCount()+1);
        tmsConfigure.setLastSentTime(new Date());
        tmsConfigureMapper.updateById(tmsConfigure);
    }

    @Override
    public void updatePostCount() {
        Long transferCount = tmsRootDataMapper.selectCount(new QueryWrapper<TmsRootData>().eq("status", "transfer"));
        Long sendSuccessCount = tmsRootDataMapper.selectCount(new QueryWrapper<TmsRootData>().eq("status", "sendSuccess"));
        Long sendFailCount = tmsRootDataMapper.selectCount(new QueryWrapper<TmsRootData>().eq("status", "sendFail"));
        PlatConfigure tmsConfigure = getTmsConfigure();
        tmsConfigure.setWaitSentCount(transferCount.intValue());
        tmsConfigure.setSentSuccessCount(sendSuccessCount.intValue());
        tmsConfigure.setSentFailCount(sendFailCount.intValue());
        tmsConfigureMapper.updateById(tmsConfigure);
    }

}
